package com.hgxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.entity.pojo.PowerOff;
import com.hgxd.entity.res.ResOptions;
import com.hgxd.entity.vo.PowerOffVo;
import com.hgxd.mapper.PowerOffMapper;
import com.hgxd.service.IPowerOffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 停送电管理 服务实现类
 * </p>
 *
 * @author wtf
 * @since 2023-11-30
 */
@Service("PowerOffServiceImpl")
public class PowerOffServiceImpl extends ServiceImpl<PowerOffMapper, PowerOff> implements IPowerOffService {

    @Resource
    private PowerOffMapper powerOffMapper;

    @Override
    public Page<PowerOff> findPage(PowerOffVo powerOffVo) {
        LambdaQueryWrapper<PowerOff> queryWrapper = getQueryWrapper(powerOffVo);
        return powerOffMapper.selectPage(new Page<>(powerOffVo.getPageNum(), powerOffVo.getPageSize())
                , queryWrapper);
    }

    @Override
    public List<ResOptions> getOperate() {
        return powerOffMapper.getOperate();
    }

    /**
     * 根据查询条件，分 1：一级审批人，2：二级审批人，3：申请人 <p>按需求查询</p>
     *
     * @param powerOffVo 需要的查询条件
     * @return 已经封装好的queryWrapper
     */
    private LambdaQueryWrapper<PowerOff> getQueryWrapper(PowerOffVo powerOffVo) {
        //如果operateId为空，就默认置为0
        if (powerOffVo.getOperateId()==null){
            powerOffVo.setOperateId(0);
        }
        LambdaQueryWrapper<PowerOff> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.
                //模糊计划名称
                like(!"".equals(powerOffVo.getPlanName())
                        , PowerOff::getPlanName
                        , powerOffVo.getPlanName())
                //时间段
                .between(!"".equals(powerOffVo.getBeginTime()) || !"".equals(powerOffVo.getEndingTime())
                        , PowerOff::getCreatTime
                        , powerOffVo.getBeginTime(), powerOffVo.getEndingTime())
                //操作人id
                .eq(powerOffVo.getOperateId() != 0
                        , PowerOff::getOperateId
                        , powerOffVo.getOperateId())
                //当前状态
                .eq(!"".equals(powerOffVo.getPlanStatus())
                        , PowerOff::getPlanStatus
                        , powerOffVo.getPlanStatus());
        // 1：一级审批人，2：二级审批人，3：申请人
        switch (powerOffVo.getType()) {
            // 二级已审批，一级才能看到，且看不了已取消的
            case 1:
                queryWrapper.eq(PowerOff::getSecondaryApprovalStatus, 0)
                        .ne(PowerOff::getPlanStatus, "已取消")
                        .ne(PowerOff::getSecondaryApprovalStatus, 2);
                break;
            // 二级看不到已取消的
            case 2:
                queryWrapper.ne(PowerOff::getPlanStatus, "已取消");
                break;
            default:
                return queryWrapper;
        }
        return queryWrapper;
    }


}
